package geradatawarehouse.dao.oracle.abs;

import geradatawarehouse.bean.abs.AbstractAluno;
import geradatawarehouse.dao.abs.AbstractDAO;

import java.io.IOException;
import java.sql.SQLException;

public abstract class AbstractAlunoOracleDAO<T extends AbstractAluno> extends
		OracleDAO {

	private static final String ANO_SEM_SQL_FLAG = "/*AnoSem*/";
	protected final int anoSem;
	protected final String selectAllQuery;

	public AbstractAlunoOracleDAO(final int anoSem,
			final String selectAllQueryFile) throws IOException {
		this.anoSem = anoSem;

		this.selectAllQuery = AbstractDAO.loadQueryFile(selectAllQueryFile)
				.replace(AbstractAlunoOracleDAO.ANO_SEM_SQL_FLAG,
						"" + this.anoSem);
	}

	public void executeSelectAll() throws SQLException {
		this.resultSet = this.querySelect(this.selectAllQuery);
	}

	/**
	 * @param a
	 * @throws SQLException
	 */
	protected void fillAbstractAluno(final AbstractAluno a) throws SQLException {
		a.setAnoSem(this.anoSem);
		a.setBairroAluno(this.resultSet.getString("BAIRROALUNO"));
		a.setCepAluno(this.resultSet.getString("CEPALUNO"));
		a.setCidadeAluno(this.resultSet.getString("CIDADEALUNO"));
		a.setCodAluno(this.resultSet.getString("CODALUNO"));
		a.setCodEscola(this.resultSet.getString("CODESCOLA"));
		a.setCre(this.resultSet.getString("CRE"));
		a.setDtNascAluno(this.parseDate(this.resultSet.getString("DTNASCALUNO")));
		a.setEndAluno(this.resultSet.getString("ENDALUNO"));
		a.setEscolaOnline(this.parseBooleanStringEqualsInsensitive(
				this.resultSet.getString("ESCOLAONLINE"), "s"));
		a.setIsMasculino(this.parseBooleanStringEqualsInsensitive(
				this.resultSet.getString("SEXOALUNO"), "m"));
		a.setLogrAluno(this.resultSet.getString("LOGRALUNO"));
		a.setNomeAluno(this.resultSet.getString("NOMEALUNO"));
		a.setNomeEscola(this.resultSet.getString("NOMEESCOLA"));
		a.setNomeMae(this.resultSet.getString("NOMEMAE"));
		a.setNumChamada(this.resultSet.getInt("NUMCHAMADA"));
		a.setTelMaeAluno(this.resultSet.getString("TELMAEALUNO"));
		a.setTurno(this.resultSet.getString("TURNO").charAt(0));
		a.setUfAluno(this.resultSet.getString("UFALUNO"));
	}

	public abstract T next() throws SQLException;

}